package cz.muni.fi.pa165.library.web_layer.adminPages.Borrow;

import cz.muni.fi.pa165.library.api.BorrowService;
import cz.muni.fi.pa165.library.api.BorrowTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.Template;
import java.util.Date;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author Filip Ligac
 */
public class EditBorrow extends Template {

    private static final long serialVersionUID = 60L;
    private BorrowTO borrow;

    public EditBorrow(final PageParameters parameters) {
        super(parameters);
        BorrowService borrowService = (BorrowService) ApplicationContextProvider.getApplicationContext().getBean("borrowService");
        StringValue borrowId = parameters.get("borrowId");
        borrow = borrowService.findBorrowByID(borrowId.toLong());

        Form<?> form = new Form("form") {
            @Override
            protected void onSubmit() {
                BorrowService borrowService = (BorrowService) ApplicationContextProvider.getApplicationContext().getBean("borrowService");
                borrowService.updateExpirationDate(borrow, borrow.getExpirationDate());
                setResponsePage(ShowAllBorrow.class);
            }
        };
        add(form);

        DateTextField expirationDateTextField = new DateTextField("expirationdate", new PropertyModel<Date>(
                borrow, "expirationDate"));
        DatePicker expirationDatePicker = new DatePicker();
        expirationDatePicker.setShowOnFieldClick(true);
        expirationDatePicker.setAutoHide(true);
        expirationDateTextField.add(expirationDatePicker);
        form.add(expirationDateTextField);

        BookmarkablePageLink homeLink = new BookmarkablePageLink<>(
                "listborrow", ShowAllBorrow.class);
        add(homeLink);
    }
}